package comparenacosconfig;

import cn.hutool.core.util.StrUtil;
import file.FileUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CompareNacosConfig {
    static String baseOutput="D:\\临时\\config比对\\";
    static String[] groupIds={"prophet","cipos","eios"};
    static String oldDbUrl="jdbc:mysql://localhost:3306/nacos_z?serverTimezone=Asia/Shanghai",oldDbUser="root",oldDbPass="root";
    static String newDbUrl="jdbc:mysql://localhost:3306/nacos_c?serverTimezone=Asia/Shanghai",newDbUser="root",newDbPass="root";

    public static void main(String[] args) throws SQLException {
        try(Connection newConn= DriverManager.getConnection(newDbUrl,newDbUser,newDbPass);
            Connection oldConn=DriverManager.getConnection(oldDbUrl,oldDbUser,oldDbPass)){
            List<String> newDataIds=selectDataIds(newConn, groupIds);//new db 的dataid
            List<String> oldDataIds=selectDataIds(oldConn, groupIds);//old db 的dataid

            compareDataIds(newDataIds,oldDataIds);

            for (String dataId : newDataIds) {
                String newContent=selectConfigContent(newConn, dataId);
                if (StrUtil.isBlank(newContent)){
                    continue;
                }
                String oldContent=selectConfigContent(oldConn, dataId);
                if (StrUtil.isBlank(oldContent)){
                    continue;
                }
                List<String> newConfigList=parseConfigContent(newContent);
                List<String> oldConfigList=parseConfigContent(oldContent);
                compareConfigContent(dataId,newConfigList, oldConfigList);
            }
        }
    }

    private static void compareConfigContent(String dataId, List<String> newConfigList, List<String> oldConfigList) {
        String outputFileName=dataId+".txt";
        FileUtil.write(baseOutput+outputFileName, "","# 应新增的配置项: ");
        for (String newConf : newConfigList) {
            if(checkNotExist(newConf, oldConfigList)) {
                FileUtil.write(baseOutput+outputFileName, newConf);
            }
        }
        FileUtil.write(baseOutput+outputFileName, "","# 应移除的配置项: ");
        for (String oldConf : oldConfigList) {
            if(checkNotExist(oldConf, newConfigList)) {
                FileUtil.write(baseOutput+outputFileName, oldConf);
            }
        }
    }

    private static boolean checkNotExist(String confInfo, List<String> confContentList) {
        String confKey=confInfo.split("=")[0];
        for (String confItem : confContentList) {
            if (StrUtil.startWith(confItem, confKey)){
                return false;
            }
        }
        return true;
    }

    private static List<String> parseConfigContent(String configText) {
        List<String> list=new ArrayList<>();
        String[] lines = configText.split("\\r?\\n");

        for (String line : lines) {
            // 忽略空行和注释
            if (StrUtil.isBlank(line) || line.trim().startsWith("#")) {
                continue;
            }

            list.add(line);
        }
        return list;
    }

    private static String selectConfigContent(Connection conn, String dataId) throws SQLException {
        String sql="select content from config_info where data_id='"+dataId+"' and group_id='"+getGroupIdByDataId(dataId)+"'";
        String content="";
        try(Statement stat=conn.createStatement();
            ResultSet rs=stat.executeQuery(sql)){
            while (rs.next()){
                content=rs.getString("content");
            }
        }
        return content;
    }

    private static String getGroupIdByDataId(String dataId) {
        if (Arrays.asList("applications","base-components","settings-frontend").contains(dataId)){
            return "prophet";
        }
        if (StrUtil.startWith(dataId,"cipos")){
            return "cipos";
        }
        if (StrUtil.startWith(dataId,"eios")||"data-api".equalsIgnoreCase(dataId)){
            return "eios";
        }
        return "";
    }

    private static void compareDataIds(List<String> newDataIds, List<String> oldDataIds) {
        String outputFileName="应新增或移除的dataId.txt";
        FileUtil.write(baseOutput+outputFileName, "","# 应新增的dataId: ");
        for (String newDataId : newDataIds) {
            if (!oldDataIds.contains(newDataId)){
                FileUtil.write(baseOutput+outputFileName, newDataId);
            }
        }
        FileUtil.write(baseOutput+outputFileName, "","# 应移除的dataId: ");
        for (String oldDataId : oldDataIds) {
            if (!newDataIds.contains(oldDataId)){
                FileUtil.write(baseOutput+outputFileName, oldDataId);
            }
        }
    }

    private static List<String> selectDataIds(Connection conn, String[] groupIds) throws SQLException {
        String sql="select data_id from config_info where group_id in ('"+ StrUtil.join("','",groupIds)+"')";
        List<String> list=new ArrayList<>();
        try(Statement stat=conn.createStatement();
            ResultSet rs=stat.executeQuery(sql)){
            while (rs.next()){
                list.add(rs.getString("data_id"));
            }
        }
        return list;
    }


}
